package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.util.ArrayList;

public class Output extends JFrame {

	private JPanel contentPane;
	JLabel lblIPCValue;
	JLabel lblExecutionTimeValue;
	JLabel lblHitRatioCache1I;
	ProgramAndData pnd;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	ArrayList<JLabel> cacheLabelsList = new ArrayList<JLabel>();

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Output frame = new Output(null);
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Output(ProgramAndData p) {
		setTitle("Simulation Output");
		pnd=p;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblIpc = new JLabel("IPC");
		lblIpc.setBounds(10, 35, 46, 19);
		contentPane.add(lblIpc);
		
		JLabel lblNumberOfCycles = new JLabel("Exec Time");
		lblNumberOfCycles.setBounds(10, 76, 60, 19);
		contentPane.add(lblNumberOfCycles);
		
		double ipcValue = pnd.hwo.mh.tomasulo.simulator.program.length*1.0/pnd.hwo.mh.tomasulo.finalCycle*1.0;
		lblIPCValue = new JLabel(""+ipcValue);
		lblIPCValue.setBounds(94, 35, 73, 19);
		contentPane.add(lblIPCValue);
		
		JLabel lblExecutionTimeValue = new JLabel(""+pnd.hwo.mh.tomasulo.finalCycle);
		lblExecutionTimeValue.setBounds(94, 76, 73, 19);
		contentPane.add(lblExecutionTimeValue);
		
		JLabel lblHitRatio = new JLabel("Hit ratios");
		lblHitRatio.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblHitRatio.setBounds(66, 106, 60, 19);
		contentPane.add(lblHitRatio);
		
		//lblHitRatioValue = new JLabel(""+getRatios());
		lblHitRatioCache1I = new JLabel("Cache 1 Ins: "+pnd.hwo.mh.tomasulo.simulator.caches[0].getHitRatio());
		lblHitRatioCache1I.setBounds(10, 138, 143, 14);
		contentPane.add(lblHitRatioCache1I);
		
		JLabel label = new JLabel("Registers");
		label.setBounds(296, 11, 107, 14);
		contentPane.add(label);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setText(""+pnd.hwo.mh.tomasulo.simulator.registerFile[1]);
		textField.setColumns(10);
		textField.setBounds(297, 36, 68, 20);
		contentPane.add(textField);
		
		JLabel label_1 = new JLabel("R1");
		label_1.setBounds(266, 39, 46, 14);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("R2");
		label_2.setBounds(266, 70, 46, 14);
		contentPane.add(label_2);
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setText(""+pnd.hwo.mh.tomasulo.simulator.registerFile[2]);
		textField_1.setColumns(10);
		textField_1.setBounds(297, 67, 68, 20);
		contentPane.add(textField_1);
		
		JLabel label_3 = new JLabel("R3");
		label_3.setBounds(266, 101, 46, 14);
		contentPane.add(label_3);
		
		textField_2 = new JTextField();
		textField_2.setEditable(false);
		textField_2.setText(""+pnd.hwo.mh.tomasulo.simulator.registerFile[3]);
		textField_2.setColumns(10);
		textField_2.setBounds(297, 98, 68, 20);
		contentPane.add(textField_2);
		
		JLabel label_4 = new JLabel("R4");
		label_4.setBounds(266, 132, 46, 14);
		contentPane.add(label_4);
		
		textField_3 = new JTextField();
		textField_3.setEditable(false);
		textField_3.setText(""+pnd.hwo.mh.tomasulo.simulator.registerFile[4]);
		textField_3.setColumns(10);
		textField_3.setBounds(297, 129, 68, 20);
		contentPane.add(textField_3);
		
		JLabel label_5 = new JLabel("R5");
		label_5.setBounds(266, 163, 46, 14);
		contentPane.add(label_5);
		
		textField_4 = new JTextField();
		textField_4.setEditable(false);
		textField_4.setText(""+pnd.hwo.mh.tomasulo.simulator.registerFile[5]);
		textField_4.setColumns(10);
		textField_4.setBounds(297, 160, 68, 20);
		contentPane.add(textField_4);
		
		JLabel label_6 = new JLabel("R6");
		label_6.setBounds(266, 194, 46, 14);
		contentPane.add(label_6);
		
		textField_5 = new JTextField();
		textField_5.setEditable(false);
		textField_5.setText(""+pnd.hwo.mh.tomasulo.simulator.registerFile[6]);
		textField_5.setColumns(10);
		textField_5.setBounds(297, 191, 68, 20);
		contentPane.add(textField_5);
		
		JLabel label_7 = new JLabel("R7");
		label_7.setBounds(266, 225, 46, 14);
		contentPane.add(label_7);
		
		textField_6 = new JTextField();
		textField_6.setEditable(false);
		textField_6.setText(""+pnd.hwo.mh.tomasulo.simulator.registerFile[7]);
		textField_6.setColumns(10);
		textField_6.setBounds(297, 222, 68, 20);
		contentPane.add(textField_6);
		
		JLabel lblHitRatioCache1D = new JLabel("Cache 1 Data: "+pnd.hwo.mh.tomasulo.simulator.caches[1].getHitRatio());
		lblHitRatioCache1D.setBounds(10, 166, 143, 14);
		contentPane.add(lblHitRatioCache1D);
		
		JLabel lblHitRatioCache2 = new JLabel("");
		lblHitRatioCache2.setBounds(10, 197, 143, 14);
		cacheLabelsList.add(lblHitRatioCache2);
		contentPane.add(lblHitRatioCache2);
		
		JLabel lblHitRatioCache3 = new JLabel("");
		lblHitRatioCache3.setBounds(10, 225, 143, 14);
		cacheLabelsList.add(lblHitRatioCache3);
		contentPane.add(lblHitRatioCache3);
		
		getRatios();
		
		this.setVisible(true);
	}

	private void getRatios() {
		String ratios = "";
		for (int i = 2; i < pnd.hwo.mh.simulator.caches.length; i++) {
			//ratios+= pnd.hwo.mh.tomasulo.simulator.caches[i]+"\n";
			cacheLabelsList.get(i-2).setText("Cache"+ i+": " + pnd.hwo.mh.tomasulo.simulator.caches[i].getHitRatio());
		}
	}
}
